import MSLogger
import MSData

def parseCfgLine(CfgLine):
    s = CfgLine.strip().split(' ')
    e = {}
    for prop in s[0:len(s)]:
        prop = prop.split('=')
        e[prop[0]] = prop[1]
    return e

def readProcessorConfig(Processor, ConfigFName):
    try:
        f = open(ConfigFName, 'r')
        cdata = f.readlines()
        for s in cdata:
            try:
                e = parseCfgLine(s)
                if e['enable'] != 'yes':
                    continue
                if   e['module'] == 'staticFirebird':
                    Processor.dbFirebirdStaticParams = (e['db_path'], e['db_user'], e['db_pass'])
                elif e['module'] == 'staticMySQL':
                    Processor.dbMySQLStaticParams = (e['db_host'], e['db_name'], e['db_user'], e['db_pass'])
                elif e['module'] == 'mobileMySQL':
                    Processor.dbMySQLMobileParams = (e['db_host'], e['db_name'], e['db_user'], e['db_pass'])
                elif e['module'] == 'objectStorage':
                    Processor.storageRoot = e['storage_root']
                elif e['module'] == 'cesarSender':
                    Processor.cesarConnectionParam = (e['host'], e['port'])
            except (Exception), err:
                MSLogger.logError('failed to parse Processor config line: %s exception: %s' % (s, str(err)))
                continue
    except (Exception), err:
            MSLogger.logError('failed to parse Processor config at all with exception: %s' % (str(err)))
            Processor.dbFirebirdStaticParams = None
            Processor.dbMySQLStaticParams = None
            Processor.dbMySQLMobileParams = None

def readObjectsConfig(ConfigFName):
    objects = []
    try:
        f = open(ConfigFName, 'r')
        cdata = f.readlines()
        for s in cdata:
            try:
                e = parseCfgLine(s)
                objId       = int(e['obj_id'])
                objName     = e['obj_name']
                objSkey     = e['obj_skey']
		o = MSData.MSObject()
                o.objectName = objName
                o.objectSKey = objSkey
                o.objectPCNId = objId
                objects.append(o)
            except (Exception), err:
                MSLogger.logError('failed to parse Object config line: %s exception: %s' % (s, str(err)))
                #TBD
                continue
    except (Exception), err:
            MSLogger.logError('failed to parse Object config at all with exception: %s' % (str(err)))
    return objects
